﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import reprise.commands.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.world.*;

    public class VisitFriendCommand extends Command {

        public var event:InfoEvent;
        public var userModel:UserModel;
        public var tickModel:TickModel;
        public var world:WorldModel;
        public var visitingFriendModel:VisitingFriendModel;
        public var friendsModel:FriendsModel;
        public var sessionModel:SessionModel;
        public var worldModel:WorldModel;
        public var energyModel:EnergyModel;
        public var gameViewModel:GameViewModel;

        override public function execute():void{
            this.tickModel.pause = true;
            this.userModel.visitingFriendID = this.event.id;
            dispatch(new Event(EventConstants.DISABLE_GAME_VIEW_NAVIGATION));
            dispatch(new Event(EventConstants.SHOW_VISIT_FRIEND_TRANSITION));
            dispatch(new InfoEvent(EventConstants.DISABLE_SCROLL_ITEM, this.userModel.visitingFriendID));
            dispatch(new Event(EventConstants.HIDE_HOSPITAL_DISPLAY));
            dispatch(new Event(EventConstants.DISABLE_FRIENDS_MENU));
            dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, null));
            dispatch(new Event(EventConstants.REMOVE_HIDDEN_BIRD));
            dispatch(new Event(EventConstants.REMOVE_ALL_ACTORS));
            var _local1:TimerCommand = new TimerCommand(400);
            _local1.addEventListener(Event.COMPLETE, this.delay_comlete);
            _local1.execute();
        }
        private function delay_comlete(_arg1:Event):void{
            var _local2:TimerCommand = TimerCommand(_arg1.target);
            _local2.removeEventListener(Event.COMPLETE, this.delay_comlete);
            _local2 = null;
            if (this.userModel.isVisitFriendsMode()){
                this.adjustViewForFriendsHospital();
            } else {
                this.adjustViewForOwnHospital();
            };
            this.sendRequest(this.userModel.visitingFriendID);
        }
        private function sendRequest(_arg1:String):void{
            var _local2:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onResult);
            _local2.addRequest("visit_friend", {id:_arg1});
            dispatch(_local2);
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:InfoEvent;
            if (_arg1.success){
                _local2 = _arg1.data.visit_friend;
                this.checkChangedUserData(_local2.changes);
                dispatch(new Event(EventConstants.RESET_HOSPITAL_DISPLAY));
                dispatch(new Event(EventConstants.INIT_HOSPITAL_DISPLAY));
                dispatch(new Event(EventConstants.ENABLE_GAME_VIEW_NAVIGATION));
                this.sessionModel.slotMachineInventoryItems = _local2.items;
                _local3 = new InfoEvent(EventConstants.INIT_WORLD, null, _local2);
                dispatch(_local3);
                this.storeFriendsData(_local2);
            };
        }
        private function checkChangedUserData(_arg1:Object):void{
            var _local5:Object;
            var _local2:Array = [];
            var _local3:int = (_arg1.xp_new - this.userModel.xpPoints);
            if (_local3 > 0){
                _local2.push({amount:_local3, type:"xp"});
            };
            var _local4:int = (_arg1.energy.value - this.energyModel.energy);
            _local4 = (_local4 + (_arg1.energy.extra - this.energyModel.energyExtra));
            if (_local4 > 0){
                _local2.push({amount:_local4, type:GameConstants.ENERGY});
            };
            if ((((_local2.length > 0)) && (this.userModel.isVisitFriendsMode()))){
                _local5 = new Object();
                _local5.friend = this.friendsModel.getFriend(this.event.id);
                _local5.items = _local2;
                _local5.userdata = _arg1;
                dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.VISIT_FRIEND_POPUP, _local5));
            };
        }
        private function storeFriendsData(_arg1:Object):void{
            this.visitingFriendModel.coins = _arg1.coins;
            this.visitingFriendModel.xpPoints = _arg1.xp;
            this.visitingFriendModel.level = _arg1.level_info.level;
            this.visitingFriendModel.numberOfCuredPets = _arg1.level_info.number_of_cured_pets;
            dispatch(new Event(EventConstants.FRIENDS_MODEL_CHANGED));
            if (this.friendsModel.hasHiddenBird(this.userModel.visitingFriendID)){
                dispatch(new Event(EventConstants.ADD_HIDDEN_BIRD));
            };
            dispatch(new InfoEvent(EventConstants.INIT_TREE, null, _arg1.tree));
        }
        private function adjustViewForFriendsHospital():void{
            this.gameViewModel.setGameScaleByIndex(GameViewModel.SCALE_4X4);
            dispatch(new Event(EventConstants.CENTER_GAMEVIEW));
            dispatch(new Event(EventConstants.SHOW_VISIT_FRIENDS_PANEL));
            dispatch(new Event(EventConstants.DISABLED_INVENTORY));
            dispatch(new Event(EventConstants.DISABLE_PATIENT_TAB));
            dispatch(new Event(EventConstants.HIDE_PATIENT_TAB_BUTTON));
            dispatch(new Event(EventConstants.DISABLE_SHOP_TABS));
            dispatch(new Event(EventConstants.HIDE_SHOP_TAB_BUTTON));
            dispatch(new Event(EventConstants.DISABLE_VILLAGE));
            dispatch(new Event(EventConstants.HIDE_MISSIONS));
            dispatch(new Event(EventConstants.HIDE_VILLAGE_ANIMATION));
        }
        private function adjustViewForOwnHospital():void{
            dispatch(new InfoEvent(EventConstants.SCROLL_TO_ID_IN_FRIENDS_MENU, this.userModel.mySocialID));
            dispatch(new Event(EventConstants.HIDE_VISIT_FRIENDS_PANEL));
            dispatch(new Event(EventConstants.ENABLED_INVENTORY));
            dispatch(new Event(EventConstants.ENABLE_PATIENT_TAB));
            dispatch(new Event(EventConstants.SHOW_PATIENT_TAB_BUTTON));
            dispatch(new Event(EventConstants.ENABLE_SHOP_TABS));
            dispatch(new Event(EventConstants.SHOW_SHOP_TAB_BUTTON));
            dispatch(new Event(EventConstants.ENABLE_VILLAGE));
            dispatch(new InfoEvent(EventConstants.INIT_MISSION));
            dispatch(new Event(EventConstants.SHOW_VILLAGE_ANIMATION));
            this.tickModel.pause = false;
        }

    }
}//package net.wooga.pets.controllers.world 
